#include <stdio.h>

int right[ 1000 ], down[ 1000 ][ 1000 ], map[ 1000 ][ 1000 ], sum[ 1000 ][ 1000 ];

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        int m, n, x, y, i, j, max;
        scanf("%d%d%d%d", &m, &n, &x, &y);
        for ( i = 0; i < m; i++ )
            for ( j = 0; j < n; j++ )
                scanf("%d", &map[ i ][ j ]);
        for ( i = 0; i < m; i++ )
        {
            right[ i ] = 0;
            for ( j = 0; j < y; j++ )
                right[ i ] += map[ i ][ j ];
        }
        for ( i = 0; i < n; i++ )
        {
            down[ 0 ][ i ] = 0;
            for ( j = 0; j < x; j++ )
                down[ 0 ][ i ] += map[ j ][ i ];
            for ( j = x; j < m; j++ )
                down[ j - x + 1 ][ i ] = down[ j - x ][ i ] - map[ j - x ][ i ] + map[ j ][ i ];
        }
        sum[ 0 ][ 0 ] = 0;
        for ( i = 0; i < y; i++ )
            sum[ 0 ][ 0 ] += down[ 0 ][ i ];
        max = sum[ 0 ][ 0 ];
        for ( i = 0; i < m - x + 1; i++ )
        {
            if ( i != 0 )
            {
                sum[ i ][ 0 ] = sum[ i - 1 ][ 0 ] - right[ i - 1 ] + right[ i + x - 1 ];
                if ( max < sum[ i ][ 0 ] ) max = sum[ i ][ 0 ];
                for ( j = 1; j < n - y + 1; j++ )
                {
                    sum[ i ][ j ] = sum[ i ][ j - 1 ] - down[ i ][ j - 1 ] + down[ i ][ j + y - 1 ];
                    if ( max < sum[ i ][ j ] ) max = sum[ i ][ j ];
                }
            }
            else
            {
                for ( j = 1; j < n - y + 1; j++ )
                {
                    sum[ i ][ j ] = sum[ i ][ j - 1 ] - down[ i ][ j - 1 ] + down[ i ][ j + y - 1 ];
                    if ( max < sum[ i ][ j ] ) max = sum[ i ][ j ];
                }
            }
        }
        printf("%d\n", max);
    }
    return 0;
}
